package com.zp.base.admin.service.impl;

import com.zp.base.admin.vo.RoleVo;
import com.zp.base.admin.common.utils.PageUtils;
import com.zp.base.admin.common.utils.Query;
import com.zp.base.admin.dao.UserRoleDao;
import com.zp.base.admin.entity.MenuEntity;
import com.zp.base.admin.entity.RoleMenuEntity;
import com.zp.base.admin.entity.UserRoleEntity;
import com.zp.base.admin.service.RoleMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zp.base.admin.dao.RoleDao;
import com.zp.base.admin.entity.RoleEntity;
import com.zp.base.admin.service.RoleService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 张鹏
 */
@Service("roleService")
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {

    @Autowired
    RoleDao roleDao;
    @Autowired
    RoleMenuService roleMenuService;
    @Autowired
    UserRoleDao userRoleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RoleEntity> page = this.page(
                new Query<RoleEntity>().getPage(params),
                new QueryWrapper<RoleEntity>().like("name", params.get("name"))
        );

        return new PageUtils(page);
    }

    @Override
    public RoleEntity selectByRoleId(Long id) {
        //获取角色已经该角色的所有权限
        //获取角色信息
        RoleEntity roleEntity = roleDao.selectById(id);
        //获取该角色的权限信息
        List<RoleMenuEntity> roleMenuEntities = roleMenuService.list(new QueryWrapper<RoleMenuEntity>().eq("role_id", roleEntity.getId()));
        if (roleMenuEntities != null && roleMenuEntities.size() > 0) {
            List<MenuEntity> permissions = new ArrayList<>();
            for (RoleMenuEntity roleMenuEntity : roleMenuEntities) {
                MenuEntity menuEntity = new MenuEntity();
                menuEntity.setId(roleMenuEntity.getMenuId());
                permissions.add(menuEntity);
            }
            roleEntity.setPermissions(permissions);
        }
        return roleEntity;
    }

    @Override
    public boolean addRole(RoleVo addRoleVo) {
        try {
            RoleEntity roleEntity = new RoleEntity();
            BeanUtils.copyProperties(addRoleVo, roleEntity);

            //添加角色
            roleDao.insert(roleEntity);

            List<String> permissionId = addRoleVo.getPermissionId();
            if (permissionId != null && permissionId.size() > 0) {
                List<RoleMenuEntity> roleMenuEntities = new ArrayList<>();
                for (String s : permissionId) {
                    RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
                    roleMenuEntity.setMenuId(Long.valueOf(s));
                    roleMenuEntity.setRoleId(roleEntity.getId());
                    roleMenuEntities.add(roleMenuEntity);
                }
                //给角色添加权限
                roleMenuService.saveBatch(roleMenuEntities);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }


    }

    @Override
    public boolean updateRole(RoleVo updateRoleVo) {
        try {
            RoleEntity roleEntity = new RoleEntity();
            BeanUtils.copyProperties(updateRoleVo, roleEntity);

            //添加角色
            roleDao.updateById(roleEntity);
            //先删除之前该角色有的权限  再重新添加权限
            roleMenuService.remove(new QueryWrapper<RoleMenuEntity>().eq("role_id", roleEntity.getId()));
            // 重新添加
            List<String> permissionId = updateRoleVo.getPermissionId();
            if (permissionId != null && permissionId.size() > 0) {
                List<RoleMenuEntity> roleMenuEntities = new ArrayList<>();
                for (String s : permissionId) {
                    RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
                    roleMenuEntity.setMenuId(Long.valueOf(s));
                    roleMenuEntity.setRoleId(roleEntity.getId());
                    roleMenuEntities.add(roleMenuEntity);
                }
                //给角色添加权限
                roleMenuService.saveBatch(roleMenuEntities);
            }

            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public Integer deleteRoleById(Long id) {
        try {
            //选判断是否有用户有该角色  有则不能删除
            List<UserRoleEntity> userRoleEntities = userRoleDao.selectList(new QueryWrapper<UserRoleEntity>().eq("role_id", id));
            if (userRoleEntities != null && userRoleEntities.size() > 0) {
                //有用户有该角色
                return 1;
            }

            //删除该角色信息以及该角色所拥有的权限信息
            roleMenuService.remove(new QueryWrapper<RoleMenuEntity>().eq("role_id", id));
            roleDao.deleteById(id);

            return 0;
        } catch (Exception e) {
            log.error(e.getMessage());
            return 2;
        }
    }

}